package com.lin.controller.user;

import com.lin.common.result.Result;
import com.lin.service.OnnxService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Map;


@RequestMapping("/onnx")
@RestController
@Api(tags = "图像识别相关接口")
@Slf4j
public class OnnxController {

    @Autowired
    private OnnxService onnxService;

    @Autowired
    private CommonController commonController;

    @PostMapping("/get")
    @ApiOperation("图像识别")
    //path为图片路径
    public String getOnnx(@RequestBody MultipartFile file, String fileName){
//        fileName = "text";
        String modelPath = "lib/best.onnx";
        //加载模型
        onnxService.load(modelPath);
        try {
            Result<String> path = commonController.upload(file);
            //获取识别数据
            String filePath =onnxService.predict(path.getData(),fileName);

            //绘图
            //onnxService.showDetect(map);
            //返回数据
            //map数据类型为【x1,y1,x2.y2,准确率，识别的序号】
            log.info("簡譜圖片xml文件路徑：{}",filePath);
            return filePath;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
